﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace IMIS_ERP.Web.Head_Agent
{
    public partial class List_Offset : System.Web.UI.Page
    {

        /// <summary>页面加载事件
        ///     <remarks>2010-12-08 河北大学信管团队</remarks>
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMessage.Text = "";
            if (!IsPostBack)
            {
                if (Session["UCode"].ToString().Substring(0, 1) != "f")
                {
                    Response.Redirect("Quit.aspx?cr=no");
                }
                ViewState["condition"] = "";
                if (Request.Params["msg"] != null && Request.Params["msg"].Trim() != "")
                {
                    if (Request.Params["msg"].ToString() == "a")
                    {
                        lblMessage.Text = "原材料结算成功";
                    }
                    else if (Request.Params["msg"].ToString() == "b")
                    {
                        lblMessage.Text = "补走原材料成功";
                    }
                }

                Bind_GV();
            }
        }

        /// <summary>重写OnInit方法
        /// 	<remark>2010-12-08 河北大学信管团队 </remark>
        /// </summary>
        override protected void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Pagination1.NavigationClick += new ImageClickEventHandler(Pager_NavigationClick);
        }

        /// <summary>响应导航控件事件——重新绑定数据
        /// 	<remark>2010-12-08 河北大学信管团队 </remark>
        /// </summary>
        private void Pager_NavigationClick(object sender, ImageClickEventArgs e)
        {
            Bind_GV();
        }

        /// <summary>单击按钮事件：查询
        ///     <remarks>2010-12-08 河北大学信管团队</remarks>
        /// </summary>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            if (ckbIsIn.Checked)
            {
                ViewState["condition"] += " and " + ddlSearch.SelectedValue + " like '%" + txtSearch.Text.Trim() + "%'";
            }
            else
            {
                ViewState["condition"] = ddlSearch.SelectedValue + " like '%" + txtSearch.Text.Trim() + "%'";
            }
            Bind_GV();
        }

        /// <summary>绑定数据：GridView
        ///     <remarks>2010-12-08 河北大学信管团队</remarks>
        /// </summary>
        public void Bind_GV()
        {
            IMIS_ERP.BLL.Pub_GetData bsns = new IMIS_ERP.BLL.Pub_GetData();
            Model.Pub_GetData model = new Model.Pub_GetData();
            //需要查询的表
            model.TableName = "Head_AgentOffset";
            //需要查询的字段
            model.FieldList = "*";
            //作为分页的字段
            model.PrimaryKey = "haID";
            //查询条件
            model.Condition = ViewState["condition"].ToString();
            //当前页
            model.PageIndex = Pagination1.PageCurrent;
            lblPageNow.Text = Pagination1.PageCurrent.ToString();
            //获得数据
            DataSet ds = bsns.GetPage(model);
            //取得总页数
            Pagination1.PageSum = Convert.ToInt32(ds.Tables[1].Rows[0]["pageSum"]);
            lblPageCount.Text = ds.Tables[1].Rows[0]["pageSum"].ToString();
            lblRecordCount.Text = ds.Tables[1].Rows[0]["recordSum"].ToString();
            //为GridView绑定赋予数据源
            GV.DataSource = ds.Tables[0].DefaultView;
            //为GridView绑定数据
            GV.DataBind();
        }

        /// <summary>响应GridView事件：创建数据行时
        /// 	<remark>2010-12-08 BY 河北大学信管团队</remark>
        /// </summary>
        protected void GV_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //为数据行增加鼠标移动到行特性
                e.Row.Attributes.Add("onmouseover", "c=style.backgroundColor;this.style.backgroundColor='" + IMIS_ERP.BLL.Assistant.getColor1 + "'");
                //为数据行增加鼠标离开行特性
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }

        protected void GV_RowEditing(object sender, GridViewEditEventArgs e)
        {
            if (GV.Rows[e.NewEditIndex].Cells[2].Text == "补走")
            {
                GV.EditIndex = e.NewEditIndex;
                Bind_GV();
            }
            else
            {
                lblMessage.Text = "原材料的结算记录不能进行修改";
            }
        }

        protected void GV_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GV.EditIndex =-1;
            Bind_GV();
        }

        protected void GV_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            IMIS_ERP.Model.Head_AgentOffset model = new IMIS_ERP.Model.Head_AgentOffset();
            model.haID = int.Parse(GV.DataKeys[e.RowIndex][0].ToString());
            model.haPcID = ((TextBox)GV.Rows[e.RowIndex].Cells[0].FindControl("txthaPcID")).Text.Trim();
            model.haNum = int.Parse(((TextBox)GV.Rows[e.RowIndex].Cells[0].FindControl("txthaNum")).Text.Trim());
            model.haWeight = decimal.Parse(((TextBox)GV.Rows[e.RowIndex].Cells[0].FindControl("txthaWeight")).Text.Trim());
            model.haPrice = decimal.Parse(((TextBox)GV.Rows[e.RowIndex].Cells[0].FindControl("txthaPrice")).Text.Trim());
            model.haFee = decimal.Parse(((TextBox)GV.Rows[e.RowIndex].Cells[0].FindControl("txthaFee")).Text.Trim());
            //model.haFeeNow = decimal.Parse(((TextBox)GV.Rows[e.RowIndex].Cells[0].FindControl("txthaFeeNow")).Text.Trim());
            model.haFeeNow = 0;
            model.haRemark = ((TextBox)GV.Rows[e.RowIndex].Cells[0].FindControl("txthaRemark")).Text.Trim();

            IMIS_ERP.BLL.Head_AgentOffset bll = new IMIS_ERP.BLL.Head_AgentOffset();
            if (bll.Update(model))
            {
                lblMessage.Text = "修改信息成功";
                GV.EditIndex = -1;
                Bind_GV();
            }
            else
            {
                lblMessage.Text = "修改信息失败,请重试";
            }

        }
    }
}